import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:smart_translator/app/const/AppConst.dart';
import 'package:smart_translator/app/db/DbDAOHelper.dart';
import 'package:smart_translator/app/db/database.dart';

class HistoryRecordsController extends GetxController {

  String sourceFrom = '';

  RxList<TranslationHistoryData>  translateHistory = RxList();



  TextEditingController searchEditeController = TextEditingController();
  var showSearchResultList = false.obs;
  RxList<TranslationHistoryData>  searchDataList = RxList();

  var emptyData = false.obs;
  var searchEmptyData = false.obs;

  int dataType = 0;

  @override
  void onInit() {
    super.onInit();
    loadDataForGage();
  }

  void loadDataForGage() async {
    sourceFrom = Get.parameters['source'] ?? 'directTranslation';
    if (sourceFrom == 'directTranslation') {
      dataType = AppConst.TRANSLATOIN_TYPE_DIRECT;

      translateHistory.value = await DbDAOHelper().obtainTranslateHistoryList(dataType);
      print(' translateHistory length: ${ translateHistory.length}');
      if (translateHistory.isEmpty) {
        emptyData.value = true;
      } else {
        emptyData.value = false;
      }

    } else if (sourceFrom == 'realTranslation') {
      dataType = AppConst.TRANSLATOIN_TYPE_REALTIME;
      translateHistory.value = await DbDAOHelper().obtainTranslateHistoryList(dataType);
      print(' translateHistory length: ${ translateHistory.length}');
      if (translateHistory.isEmpty) {
        emptyData.value = true;
      } else {
        emptyData.value = false;
      }
    } else if (sourceFrom == 'listeningTranslation') {
      dataType = AppConst.TRANSLATOIN_TYPE_LISTENING;
      translateHistory.value = await DbDAOHelper().obtainTranslateHistoryList(dataType);
      print(' translateHistory length: ${ translateHistory.length}');
      if (translateHistory.isEmpty) {
        emptyData.value = true;
      } else {
        emptyData.value = false;
      }

    }
  }

  @override
  void onReady() {
    super.onReady();

  }

  @override
  void onClose() {
    super.onClose();
  }

 /* String getDateTimeByTakId(DateTime dateTime) {
    final day = dateTime.day;
    final month = dateTime.month;
    final year = dateTime.year;

    // 定义中文月份列表
    const months = ['一月', '二月', '三月', '四月', '五月', '六月',
      '七月', '八月', '九月', '十月', '十一月', '十二月'];
    return '${day.toString().padLeft(2,'0')} ${months[month - 1]} $year';
  }*/


  void searchContent() async {
    if (searchEditeController.text.trim() != '') {
      searchDataList.value = await DbDAOHelper().obtainTranslateHistoryListByMode(searchEditeController.text,dataType);
      print('historyDataList :: ${searchDataList.length}');
      showSearchResultList.value = true;
    } else {
      showSearchResultList.value = false;
    }

    if(searchDataList.isEmpty) {
      searchEmptyData.value = true;
    } else {
      searchEmptyData.value = false;
    }


  }
}
